package com.stellar.circle.basic.dao;

import com.stellar.circle.basic.entity.SPostTag;
import com.stellar.circle.controller.admin.sPostTag.vo.HotTopicVO;
import com.stellar.circle.controller.admin.sPostTag.vo.PostTagOverviewVO;
import com.stellar.circle.controller.admin.sPostTag.vo.UsageTrend;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;
import java.util.Map;

/**
 * 话题表(SPostTag)数据访问层
 *
 * @author Jqchan
 * @since 2025-05-17 19:30:33
 */
public interface SPostTagDao {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键ID
     */
    SPostTag queryById(@Param("id") Long id);

    /**
     * 条件查询单条结果
     *
     * @param condition 查询条件
     */
    SPostTag queryByCondition(@Param("condition") SPostTag condition);

    /**
     * 条件查询所有结果
     *
     * @param condition 查询条件
     */
    List<SPostTag> queryAllByCondition(@Param("condition") SPostTag condition);

    /**
     * 分页查询
     *
     * @param condition 查询条件
     * @param page      页码
     * @param pageSize  每页数量
     */
    List<SPostTag> page(@Param("condition") SPostTag condition,
                        @Param("page") int page,
                        @Param("pageSize") int pageSize);

    /**
     * 条件统计
     *
     * @param condition 统计条件
     */
    Long count(@Param("condition") SPostTag condition);

    /**
     * 新增数据
     *
     * @param entity 实例对象
     */
    int insert(@Param("entity") SPostTag entity);

    /**
     * 批量新增
     *
     * @param entities 实例列表
     */
    int insertBatch(@Param("entities") List<SPostTag> entities);

    /**
     * 更新数据
     *
     * @param entity 实例对象
     */
    int update(@Param("entity") SPostTag entity);

    /**
     * 通过主键删除
     *
     * @param id 主键ID
     */
    int deleteById(@Param("id") Long id);

    /**
     * 处理话题
     *
     * @param postTag
     * @return
     */
    @Options(statementType = StatementType.CALLABLE)
    Long callUpsertPostTag(@Param("p_tag_name") String postTag);

    /**
     * 根据id查询话题批量
     *
     * @param tagIds
     * @return
     */
    List<SPostTag> queryByIds(@Param("tagIds") List<Long> tagIds);

    /**
     * 根据话题名称查询话题
     *
     * @param tagName
     * @return
     */
    @Select("select * from s_post_tag where post_tag_name = #{tagName} and is_deleted = 0")
    SPostTag selectByTagName(String tagName);

    /**
     * 递增话题使用次数
     *
     * @param id
     * @return
     */
    @Update("update s_post_tag set usage_count = usage_count + 1 where id = #{id}")
    int incrementUsageCount(Long id);

    /**
     * 查询话题使用统计
     *
     * @return
     */
    @Select("SELECT " +
            "(SELECT COUNT(*) FROM s_post_tag WHERE is_deleted = 0) AS totalTags, " +
            "(SELECT COUNT(*) FROM s_post_tag WHERE is_global = 1 AND is_deleted = 0) AS globalTags, " +
            "(SELECT COUNT(*) FROM s_post_tag WHERE is_pinned = 1 AND is_deleted = 0) AS pinnedTags, " +
            "(SELECT SUM(usage_count) FROM s_post_tag WHERE is_deleted = 0) AS totalUsage")
    PostTagOverviewVO selectTagStats();

    /**
     * 获取话题类型分布
     *
     * @return
     */
    @Select("SELECT " +
            "SUM(CASE WHEN is_global = 1 THEN 1 ELSE 0 END) AS globalCount, " +
            "SUM(CASE WHEN is_global = 0 THEN 1 ELSE 0 END) AS schoolCount " +
            "FROM s_post_tag WHERE is_deleted = 0")
    PostTagOverviewVO selectTypeDistribution();

    /**
     * 获取使用趋势 - 最近七天
     *
     * @return
     */
    List<UsageTrend> selectRecentUsageTrend();

    /**
     * 获取热门话题排行
     *
     * @return
     */
    @Select("SELECT post_tag_name AS tagName, usage_count AS usageCount " +
            "FROM s_post_tag " +
            "WHERE is_deleted = 0 " +
            "ORDER BY usage_count DESC " +
            "LIMIT 10")
    List<HotTopicVO> selectHotTopics();
}

